package com.tju.eduservice.controller;


import com.tju.Result;
import com.tju.eduservice.client.VodClient;
import com.tju.eduservice.entity.EduVideo;
import com.tju.eduservice.service.EduVideoService;
import com.tju.serviceHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author gsc
 * @since 2021-11-29
 */
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("/addVideo")
    public Result addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return Result.ok();
    }


    //删除小节
    // TODO 后面这个方法需要完善，删除小节的时候，同时也要把视频删除
    @DeleteMapping("/deleteVideo/{id}")
    public Result deleteVideo(@PathVariable String id){
        //根据小节id得到视频id
        EduVideo eduVideo = eduVideoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){
            Result result = vodClient.removeVideo(videoSourceId);//删除视频
            if(result.getCode() == 20001){//熔断器执行
                throw new GuliException(20001,"删除视频失败,熔断器执行");
            }
        }

        eduVideoService.removeById(id);
        return Result.ok();
    }

    //修改小节
    @PostMapping("/updateVideo")
    public Result updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);

        return Result.ok();
    }

    //根据小节id查询
    @GetMapping("/getVideoById/{videoId}")
    public Result getVideoById(@PathVariable String videoId){
        EduVideo eduVideo = eduVideoService.getById(videoId);
        return Result.ok().data("video",eduVideo);
    }



}

